<?php

/**
 * This is the model class for table "order".
 *
 * The followings are the available columns in table 'order':
 * @property string $order_date
 * @property integer $id
 * @property integer $order_quantity
 * @property integer $customer_id
 * @property integer $inventory_id
 * @property string $order_price
 */
class order extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return order the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'order';
	}
	
	public function showPrice( $attribute, $params )
    {
                $this->order_price=0;
                
                                
                $this->order_price=(($this->order_quantity)* ($this->product->product_price));
                        
    }

	public function deduct ( $attribute, $params )
	{
				$this->inventory->inventory_qoh=(($this->inventory->inventory_qoh) - ($this->order_quantity));
	}
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('customer_id, product_id', 'required'),
			array('order_quantity, customer_id, product_id', 'numerical', 'integerOnly'=>true),
			array('order_price', 'length', 'max'=>10),
			array('order_price','showPrice'),
			array('order_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, order_date, order_quantity, customer_id, product_id, order_price', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'customer' => array(self::BELONGS_TO, 'customer', 'customer_id'),
		'product' =>array(self::BELONGS_TO,'product','product_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Order Number',
			'order_date' => 'Order Date',
			'order_quantity' => 'Order Quantity',
			'customer_id' => 'Customer',
			'product_id' => 'Product',
			'order_price' => 'Order Price',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('order_date',$this->order_date,true);
		$criteria->compare('order_quantity',$this->order_quantity);
		$criteria->compare('customer_id',$this->customer_id);
		$criteria->compare('product_id',$this->product_id);
		$criteria->compare('order_price',$this->order_price,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getFName()
			{       
				return $this->customer['fullname']; 
			}
			
		public function getprodesc()
			{       
				return $this->product['fulldesc']; 
			}
}